import { message } from 'antd';

import { Effect } from '@/dva/types';

import userIno from '../../service/user/info';

import userLogin from '../../service/user/login';

/*
* 用户登录
* */
export const login: Effect = async (payload, { put }) => {
  put({
    payload: true,
    type: 'user/setLoading'
  });
  try {
    put({
      type: 'user/setUserInfo',
      payload: await userLogin(payload)
    });
  } catch (e) {
    message.error(e.message);
  }
  put({
    payload: false,
    type: 'user/setLoading'
  });
}

/*
* 获取用户登录信息
* */

export const userInfo: Effect = async (payload, { put }) => {
  put({
    type: 'user/setUserInfo',
    payload: await userIno()
  });
}

/*
* 退出登录
* */
export const logout: Effect = async (payload, { put }) => {
  put({
    type: 'user/setUserInfo',
    payload: undefined
  });
}
